package com.example.administrator.dc.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.dc.Bean.Usermessage;
import com.example.administrator.dc.R;
import com.example.administrator.dc.Service.UserService;
import com.example.administrator.dc.fragment.ClubCenterFragment;
import com.example.administrator.dc.fragment.MyHomeFragment;
import com.example.administrator.dc.fragment.PeixunFragment;
import com.example.administrator.dc.fragment.StoreFragment;
import com.example.administrator.dc.helper.RoundImageView;
import com.example.administrator.dc.model.Contants;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class HomeActivity extends BaseActivity implements View.OnClickListener{
   /* private ViewPager mViewPager;
    private FragmentPagerAdapter mFragmentAdapter;
    //装载fragment的集合
   private List<Fragment> mFragments;*/

    private LinearLayout mTabStore;
    private LinearLayout mTabMyHome;
    private LinearLayout peixun;
    private LinearLayout Club;

    private ImageButton mImgStore;
    private ImageButton mImgMyHome;
    private ImageView release;
    private RoundImageView touxiang;

    private Context context;
    private Fragment mFmStore;
    private Fragment mFmMyHome;
    private Fragment ClubF;
    private  Fragment PeiF;

    private TextView mTextColorStore;
    private TextView mTextColorMyHome;

    private Toolbar mTbHeadBar;
    private  LinearLayout mLlMenu;
    private ListView mLvMenu;
    private  ImageButton imgBtn_person_center;
    private DrawerLayout mMyDrawable;
    ActionBarDrawerToggle mToggle;
    private  List<String> lvMenuList;

    private void ischeck(){
        SharedPreferences sp = getApplication().getSharedPreferences("UserInfo", getApplication().MODE_PRIVATE);
        String userid =sp.getString("user_id", "");
        if(!"".equals(userid)){
            isCheck=true;
        }
        else {
            isCheck=false;
        }
    }
    private  void  data(){
         lvMenuList = new ArrayList<String>() {{
            add("我的关注");
            add("我的收藏");
            add("我的照片");
            if(isCheck==false){
                add("登录");
            }
            else {
                add("注销");
            }

        }};

    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main2 );
        ischeck();
        data();
        initViews();
        initEvents();
        touxiang();
        setselect(1);
    }
    private  void  touxiang(){
        SharedPreferences sp = getApplication().getSharedPreferences("UserInfo", getApplication().MODE_PRIVATE);
        String userid =sp.getString("user_id", null);
        touxiang= (RoundImageView) findViewById(R.id.touxiang);
        final TextView username= (TextView) findViewById(R.id.username);
        Retrofit retrofit=new Retrofit.Builder().baseUrl(Contants.BASE_URL1).
                addConverterFactory(GsonConverterFactory.create()).build();
        UserService service=retrofit.create(UserService.class);
        Call<Usermessage> call= service.usermessage(userid);
        call.enqueue(new Callback<Usermessage>() {
            @Override
            public void onResponse(Call<Usermessage> call, Response<Usermessage> response) {
                if (!"".equals(response.body().getPic()))
                    Picasso.with(HomeActivity.this).load(response.body().getPic()).into(touxiang);
                username.setText(response.body().getAliasname());

            }

            @Override
            public void onFailure(Call<Usermessage> call, Throwable t) {

            }
        });


    }

    private void initViews(){
        ButterKnife.bind(this);
        release= (ImageView) findViewById(R.id.release);
        mTbHeadBar= (Toolbar) findViewById(R.id.tbHeadBar);
        mLlMenu= (LinearLayout) findViewById(R.id.llMenu);
        mLvMenu= (ListView) findViewById(R.id.lvMenu);



        mMyDrawable= (DrawerLayout) findViewById(R.id.dlMenu);
        mTabStore=(LinearLayout)findViewById(R.id.id_tab_store);
        mTabMyHome=(LinearLayout)findViewById(R.id.id_tab_myhome);
        peixun= (LinearLayout) findViewById(R.id.peixun);
        Club= (LinearLayout) findViewById(R.id.club);

        mImgStore=(ImageButton)findViewById(R.id.id_tab_store_img);
        mImgMyHome=(ImageButton)findViewById(R.id.id_tab_myhome_img);
     ;
        mTextColorStore=(TextView)findViewById(R.id.id_tvColor_store) ;
        mTextColorMyHome=(TextView)findViewById(R.id.id_tvColor_myhome);
        imgBtn_person_center=(ImageButton)findViewById(R.id.imgBtn_person_center);

        initToolBarAndDrawableLayout();
        release.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setClass(HomeActivity.this,UploadActivity.class);
                startActivity(intent);
            }
        });

               mLvMenu.setAdapter(new ArrayAdapter<String>(this,R.layout.menu_2,R.id.menu,lvMenuList));

        mLvMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                System.out.println(lvMenuList.get(position));
                if(lvMenuList.get(position)=="我的关注")
                {
                    Toast.makeText(getApplication(),"我的关注",Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent();
                    intent.setClass(HomeActivity.this,MyFollowActivity.class);
                    startActivity(intent);

                }
                if(lvMenuList.get(position)=="我的收藏")
                {
                    Toast.makeText(getApplication(),"我的收藏",Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent();
                    intent.setClass(HomeActivity.this,CollectActivity.class);
                    startActivity(intent);

                }
                if(lvMenuList.get(position)=="登录"){
                    Toast.makeText(getApplication(),"登录",Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent();
                    intent.setClass(HomeActivity.this,MainActivity.class);
                    startActivity(intent);
                }
                if(lvMenuList.get(position)=="注销"){
                    Toast.makeText(getApplication(),"注销成功",Toast.LENGTH_LONG).show();
                        SharedPreferences sharedPreferences=getSharedPreferences("UserInfo", MODE_PRIVATE);
                        SharedPreferences.Editor editor=sharedPreferences.edit();//获取编辑器
                        //int uid=Integer.parseInt(userid);
                        String uid="";
                        editor.putString("user_id", uid);

                   /* sharedPreferences = getSharedPreferences("UserCheck", MODE_PRIVATE);
                    editor = sharedPreferences.edit();*/
                    /* boolean is=false;
                    editor.putBoolean("check",is);*/
                    editor.commit();
                    Intent intent=new Intent();
                    intent.setClass(HomeActivity.this,MainActivity.class);
                    startActivity(intent);

                }
                if(lvMenuList.get(position)=="我的照片") {
                    Toast.makeText(getApplication(),"我的照片",Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent();
                    intent.setClass(HomeActivity.this,PhotoImage.class);
                    startActivity(intent);
                }
                mMyDrawable.closeDrawers();/*收起抽屉*/
            }
        });
        imgBtn_person_center.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplication(),"个人中心",Toast.LENGTH_SHORT).show();
                Intent intent=new Intent();
                intent.setClass(HomeActivity.this,PersonCenterActivity.class);
                startActivity(intent);
            }
        });


    }
    private void resetImgs() {
        mImgStore.setImageResource(R.drawable.shop_false);
        mImgMyHome.setImageResource(R.drawable.home_false);


    }
    private  void resetTextColor(){
        mTextColorStore.setTextColor(mTextColorStore.getResources().getColor(R.color.white));

        mTextColorMyHome.setTextColor(mTextColorMyHome.getResources().getColor(R.color.white));

    }
    public void initEvents(){
        mTabStore.setOnClickListener(this);

        mTabMyHome.setOnClickListener(this);

        Club.setOnClickListener(this);
        peixun.setOnClickListener(this);


    }
    private void initToolBarAndDrawableLayout() {
        setSupportActionBar(mTbHeadBar);
  /*以下俩方法设置返回键可用*/
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  /*设置标题文字不可显示*/
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        mToggle = new ActionBarDrawerToggle(this, mMyDrawable, mTbHeadBar, R.string.app_name, R.string.app_name) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                //Toast.makeText(MainActivity.this, R.string.open, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                //Toast.makeText(MainActivity.this, R.string.close, Toast.LENGTH_SHORT).show();
            }
        };
  /*mMyDrawable.setDrawerListener(mToggle);不推荐*/
//        mMyDrawable.addDrawerListener(mToggle);
        //       mToggle.syncState();/*同步状态*/
    }

    @Override
    public void onClick(View v) {
          resetImgs();
        resetTextColor();
        switch (v.getId())
        {
            case R.id.id_tab_store:
              setselect(0);
                break;

            case R.id.id_tab_myhome:
                setselect(1);
                break;

            case R.id.peixun:
                setselect(3);

                break;

            case R.id.club:
                setselect(2);
                break;

            default:
                break;

        }

    }

    private void setselect(int i) {
        FragmentManager fm=getSupportFragmentManager();
        FragmentTransaction ft=fm.beginTransaction();
          hideFragment(ft);
        switch (i)
        {
            case 0:
                if (mFmStore == null)
                {
                    mFmStore = new StoreFragment();
                    ft.add(R.id.id_content, mFmStore);
                } else
                {
                    ft.show(mFmStore);
                }
                mImgStore.setImageResource(R.drawable.shop_true);
                mTextColorStore.setTextColor(mTextColorStore.getResources().getColor(R.color.grayness));

                break;

                       case 1:
                if (mFmMyHome== null)
                {
                    mFmMyHome = new MyHomeFragment();
                    ft.add(R.id.id_content, mFmMyHome);
                } else
                {
                    ft.show(mFmMyHome);
                }
                mImgMyHome.setImageResource(R.drawable.home_true);
                mTextColorMyHome.setTextColor(mTextColorMyHome.getResources().getColor(R.color.grayness));
                break;
            case 2:
                if(ClubF==null) {
                    ClubF = new ClubCenterFragment();
                    ft.add(R.id.id_content, ClubF);
                }
                else {
                    ft.show(ClubF);
                }
                break;
            case 3:
                if(PeiF==null){
                    PeiF=new PeixunFragment();
                    ft.add(R.id.id_content,PeiF);
                }
                else {
                    ft.show(PeiF);
                }
            default:
                break;
        }// mViewPager.setCurrentItem(i);
        ft.commit();
    }

    private void hideFragment(FragmentTransaction ft) {
        if (mFmStore!=null)
        {
            ft.hide(mFmStore);
        }

        if (mFmMyHome!=null)
        {
            ft.hide(mFmMyHome);
        }
        if(ClubF!=null){
            ft.hide(ClubF);
        }
        if(PeiF!=null){
            ft.hide(PeiF);
        }

    }
}
